package com.mars.tukuan.rpc.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mars.base.common.base.BaseResult;
import com.mars.base.common.utils.Paginator;
import com.mars.base.common.utils.PasswordUtils;
import com.mars.base.common.utils.RandomUitl;
import com.mars.base.common.utils.SecurityUtils;
import com.mars.base.service.support.BaseService;
import com.mars.tukuan.dao.mapper.CarUserMapper;
import com.mars.tukuan.dto.CarUserVo;
import com.mars.tukuan.entity.CarUser;
import com.mars.tukuan.rpc.api.CarUserService;

@Service
public class CarUserServiceImpl extends BaseService<CarUser, Long> implements CarUserService {
	
	@Autowired
	private CarUserMapper mapper ;
	
	@Override
	public PageInfo<CarUserVo> list(Paginator paginator, CarUserVo entity) {
		PageHelper.startPage(paginator.getPage(), paginator.getRows());
		Page<CarUserVo> list = this.mapper.list(paginator, entity);
		return new PageInfo<CarUserVo>(list) ;
	}
	
	@Override
	public CarUser insertSelective(CarUser entity) {
		entity.setSalt(RandomUitl.uuid());
		//密码加密加盐，使用手机号码作为登陆账号，手机号码后4位为初始密码
		entity.setPassword(PasswordUtils.encodePassword(entity.getMobile().substring(entity.getMobile().length()-4), entity.getSalt()));
		
		return super.insertSelective(entity) ;
	}
	
	@Override
	public CarUser updateSelective(CarUser entity) {
		return super.updateSelective(entity);
	}
	
	@Override
	public CarUser selectById(Long id) {
		return super.selectById(id) ;
	}
	
	@Override
	public CarUser getUserByMobile(String mobile) {
		return this.mapper.getUserByMobile(mobile);
	}

	@Override
	public CarUser gerUserByUserName(String username) {
		CarUser entity = new CarUser() ;
		entity.setUsername(username); 
		return super.selectOne(entity) ;
	}

	@Override
	public Boolean checkData(String content, Integer type) {
		if(type == 1) {// 用户名是否存在
			CarUser checkValue = this.mapper.checkValue("username", content) ;
			return checkValue != null ? true : false ;
		} else if(type == 2) {// 邮件地址是否存在
			CarUser checkValue = this.mapper.checkValue("email", content) ;
			return checkValue != null ? true : false ;
		} else if(type == 3) {// 手机号码是否存在
			CarUser checkValue = this.mapper.checkValue("mobile", content) ;
			return checkValue != null ? true : false ;
		}
		return null;
	}
	
	@Override
	public BaseResult updatePasswordById(Long id, String mobile, String password) {
		if(null == mobile) {
			return BaseResult.build(false, "手机号码不能为空") ;
		}
		if(null == password) {
			return BaseResult.build(false, "原密码不能为空") ;
		}
		String uuid = RandomUitl.uuid() ;
		this.mapper.updatePasswordById(id, SecurityUtils.sha256(mobile+password, uuid), uuid);
		return BaseResult.build(true, "密码修改成功") ;
	}

	@Override
	public BaseResult updatePassword(String mobile, String oldPassword, String newPassword) {
		if(null == mobile) {
			return BaseResult.build(false, "手机号码不能为空") ;
		}
		if(null == oldPassword) {
			return BaseResult.build(false, "原密码不能为空") ;
		}
		if(null == newPassword) {
			return BaseResult.build(false, "新密码不能为空") ;
		}
		
		BaseResult result = validateLogin(mobile, oldPassword) ;
		if(result.getStatus()) {
			CarUser user = (CarUser) result.getData() ;
			String slat = RandomUitl.uuid() ;
			this.mapper.updatePasswordById(user.getId(), PasswordUtils.encodePassword(newPassword, slat), slat);
			return BaseResult.build(true, "密码修改成功") ;
		} else {
			return BaseResult.build(false, "原密码不正确") ;
		}
		
	}

	@Override
	public BaseResult resetPassword(Long[] ids) {
		int successCount = 0, failCount = 0;
		for (Long id : ids) {
			CarUser user = this.selectById(id);
			if(null != user) {
				String password = user.getMobile().substring(user.getMobile().length()-4) ;
				String slat = RandomUitl.uuid() ;
				String encodePassword = PasswordUtils.encodePassword(password, slat) ;
				
				Integer updatePassword = this.mapper.resetPassword(user.getId(), encodePassword, slat) ;
				if(updatePassword > 0) {
					successCount ++ ;
				} else {
					failCount ++ ;
				}
			}
		}
		Map<String, Object> mapData = new HashMap<String, Object>() ;
		mapData.put("successCount", successCount) ;
		mapData.put("failCount", failCount) ;
		return BaseResult.build(true, "重置密码", mapData) ;
		
	}

	@Override
	public Integer updateAccountStatus(Long[] ids, Integer status) {
		return this.mapper.updateAccountStatus(ids, status);
	}

	@Override
	public BaseResult validateLogin(String mobile, String password) {
		
		CarUser user = this.getUserByMobile(mobile) ;
		
		if(null != user) {
			if(user.getStatus()==2) {
				return BaseResult.build(false, "该账号已禁用，请联系管理员") ;
			}
			boolean passwordValid = PasswordUtils.isPasswordValid(user.getPassword(), password, user.getSalt()) ;
			if(passwordValid) {
				return BaseResult.build(true, "验证成功", user) ;
			} else {
				return BaseResult.build(false, "验证失败，账号或密码错误") ;
			}
		} else {
			return BaseResult.build(false, "验证失败，账号或密码错误") ;
		}
	}

	@Override
	public void updateCheckTime(Long id) {
		this.mapper.updateCheckTime(id);
	}

}
